
// ViewYourImageView.cpp : implementation of the CViewYourImageView class
//

#include "stdafx.h"
// SHARED_HANDLERS can be defined in an ATL project implementing preview, thumbnail
// and search filter handlers and allows sharing of document code with that project.
#ifndef SHARED_HANDLERS
#include "ViewYourImage.h"
#endif

#include "ViewYourImageDoc.h"
#include "ViewYourImageView.h"

#include "global.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#endif

#define DEF_TOP_VIEW_IMAGE_HEIGHT		100
#define DEF_LINE_VIEW					1
#define ID_GRP_SHOW_BIG_IMAGE			501

// CViewYourImageView

IMPLEMENT_DYNCREATE(CViewYourImageView, CFormView)

BEGIN_MESSAGE_MAP(CViewYourImageView, CFormView)
	ON_WM_CONTEXTMENU()
	ON_WM_RBUTTONUP()
	ON_WM_PAINT()
END_MESSAGE_MAP()

// CViewYourImageView construction/destruction

CViewYourImageView::CViewYourImageView()
	: CFormView(CViewYourImageView::IDD)
{

	GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);
	// TODO: add construction code here
	// TODO: add construction code here
	m_pCViewControl = 0;
	m_pBigImageControl = 0;
	g_strBigImageName = L"";
}

CViewYourImageView::~CViewYourImageView()
{
	GdiplusShutdown(gdiplusToken);
	if ( m_pCViewControl != 0 )
		delete m_pCViewControl;

	if (m_pBigImageControl != 0 )
		delete m_pBigImageControl;
}

//BOOL CViewYourImageView::PreCreateWindow(CREATESTRUCT& cs)
//{
//	// TODO: Modify the Window class or styl
//}

//CViewYourImageView::~CViewYourImageView()
//{
//}

void CViewYourImageView::DoDataExchange(CDataExchange* pDX)
{
	CFormView::DoDataExchange(pDX);
}

BOOL CViewYourImageView::PreCreateWindow(CREATESTRUCT& cs)
{
	// TODO: Modify the Window class or styles here by modifying
	//  the CREATESTRUCT cs

	return CFormView::PreCreateWindow(cs);
}

void CViewYourImageView::CreateGroupViewImage()
{
	RECT rect;
	GetClientRect(&rect);
	rect.left = rect.left +1;
	rect.top = rect.top + DEF_TOP_VIEW_IMAGE_HEIGHT + DEF_LINE_VIEW;
	// Create Group show image control 
	//CreateGroupBox(ID_GR_SHOWIMAGE,L"",CRect(5,95,980,505));
	CButton* groupBox = new CButton();
	groupBox->Create(	L"",
						WS_CHILD|WS_VISIBLE|BS_GROUPBOX, 
						rect, 
						this, 
						ID_GRP_SHOW_BIG_IMAGE);
}
void CViewYourImageView::OnInitialUpdate()
{
	CFormView::OnInitialUpdate();
	GetParentFrame()->RecalcLayout();
	ResizeParentToFit();

	// Create group view image
	CreateGroupViewImage();
	
	if ( m_pCViewControl == 0)
	{
		CString m_PathFolder = L"";
		m_PathFolder.Format(L"res\\");
		m_pCViewControl = new CTopViewImageControl;
		RECT rect;
		GetClientRect(&rect);
		m_pCViewControl->CreateViewImageControl( 10, DEF_TOP_VIEW_IMAGE_HEIGHT,10, rect, this );
		m_pCViewControl->LoadViewImage( m_PathFolder );
	}

}

void CViewYourImageView::OnRButtonUp(UINT /* nFlags */, CPoint point)
{
	ClientToScreen(&point);
	OnContextMenu(this, point);
}

void CViewYourImageView::OnContextMenu(CWnd* /* pWnd */, CPoint point)
{
#ifndef SHARED_HANDLERS
	theApp.GetContextMenuManager()->ShowPopupMenu(IDR_POPUP_EDIT, point.x, point.y, this, TRUE);
#endif
}


// CViewYourImageView diagnostics

#ifdef _DEBUG
void CViewYourImageView::AssertValid() const
{
	CFormView::AssertValid();
}

void CViewYourImageView::Dump(CDumpContext& dc) const
{
	CFormView::Dump(dc);
}

CViewYourImageDoc* CViewYourImageView::GetDocument() const // non-debug version is inline
{
	ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CViewYourImageDoc)));
	return (CViewYourImageDoc*)m_pDocument;
}
#endif //_DEBUG


// CViewYourImageView message handlers


void CViewYourImageView::OnPaint()
{
	CPaintDC dc(this); // device context for painting
	if ( g_strBigImageName != "")
	{
		RECT rect;
		GetClientRect(&rect);
		rect.left = rect.left +1;
		rect.top = rect.top + DEF_TOP_VIEW_IMAGE_HEIGHT + DEF_LINE_VIEW;

		// Make sure the big image control is NULL
		if ( m_pBigImageControl != 0 ) 
		{
			delete m_pBigImageControl;
		}
		// Always create a new Image control 
		m_pBigImageControl = new CPictureCommon;

		{
			Bitmap bitmap(g_strBigImageName ); 
			
			m_pBigImageControl->SetImageWidth(bitmap.GetWidth());
			m_pBigImageControl->SetImageHeight(bitmap.GetHeight());

			// check Image width comparing to client rect
			if ( ( rect.right - rect.left ) < bitmap.GetWidth() )
			{
				m_pBigImageControl->SetImageWidth( rect.right - rect.left );
			}
			// check Image height comparing to client rect
			if ( ( rect.bottom - rect.top ) < bitmap.GetHeight() )
			{
				m_pBigImageControl->SetImageHeight( rect.bottom - rect.top );
			}
		}
		//Bitmap *pbitmap = new Bitmap ( g_strBigImageName ); 
		
		//bitmap = new Bitmap( g_strBigImageName ); 
	//	bitmap = new Bitmap(g_strBigImageName,0 );
	//	bitmap = new Bitmap( ");
	//	CString path = L"";
	//	path.Format(L"%s",(*m_listpictureCotrl)[i]->m_szPathIMAGE);
	//	int w =  bitmap->GetWidth() ;
	//	int h =  bitmap->GetHeight();
		//(*m_listpictureCotrl)[i]->W_picture = bitmap->GetWidth();
		//(*m_listpictureCotrl)[i]->H_picture= bitmap->GetHeight();
		

		//m_pBigImageControl->Crea
		//m_pBigImageControl->CreateViewImageControl( 10, DEF_TOP_VIEW_IMAGE_HEIGHT,10, rect, this );

		int nCenterX = 0,  nCenterY = 0;
		nCenterX = ( rect.right - rect.left )/2;
		nCenterY = ( rect.bottom - rect.top )/2;

		// update the rect.top, rect.right
		rect.left = nCenterX - ( m_pBigImageControl->GetImageWidth()/2 );
		rect.top  = DEF_TOP_VIEW_IMAGE_HEIGHT + DEF_LINE_VIEW + nCenterY - ( m_pBigImageControl->GetImageHeight()/2 );

		rect.bottom = rect.top +  m_pBigImageControl->GetImageHeight();
		rect.right = rect.left +  m_pBigImageControl->GetImageWidth();
		m_pBigImageControl->Create(_T(""),
		WS_CHILD | WS_VISIBLE | BS_ICON | SS_NOTIFY,
		rect,
		this,
		1);
		

		m_pBigImageControl->CreateImageFromFile( g_strBigImageName );

		g_strBigImageName = L"";
		

	//	delete bitmap ;
	}
	//if m_pBigImageControl
		// TODO: Add your message handler code here
		// Do not call CFormView::OnPaint() for painting messages
}
